package com.karl.loading.loadings;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.karl.loading.base.BaseProgressView;

/**
 * Created by Karl on 2017/8/25 0025.
 * desc: 仿AliPay自定义控件
 */

public class AliPayLoadingView extends BaseProgressView {

    private Paint mCirclePaint;

    private Paint mArcPaint;

    private RectF mRectF;

    private float progress;

    private float degree;

    public AliPayLoadingView(Context context) {
        this(context,null);
    }

    public AliPayLoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private void initPaint(){
        setMaxProgress(360);
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(dp2px(DEFAULT_STROKE_WIDTH));
        mCirclePaint.setColor(Color.parseColor("#bababa"));

        mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeWidth(dp2px(DEFAULT_STROKE_WIDTH));
        mArcPaint.setColor(Color.RED);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = getWidth()/2;
        int y = getHeight()/2;

        canvas.translate(x,y);
        //初始化RectF
        if (mRectF == null){
            mRectF = new RectF(-x+ defaultPadding,-y+ defaultPadding,x- defaultPadding,y- defaultPadding);
        }

        canvas.drawCircle(0,0,x- defaultPadding,mCirclePaint);

        switch (mCurrentMode){
            //下拉状态
            case MODE_PROGRESS:
//                canvas.drawCircle(0,0,x-defaultPadding,mCirclePaint);
                canvas.drawArc(mRectF,-90,progress,false,mArcPaint);
                break;
            case MODE_LOADING:
                canvas.rotate(degree);
                canvas.drawArc(mRectF,-60,30,false,mArcPaint);
                break;
        }


    }

    @Override
    public void updateProgress(int progress) {
        super.updateProgress(progress);
        if (progress > getMaxProgress()){
            progress = getMaxProgress();
        }
        this.progress = progress;
        postInvalidate();
    }

    @Override
    public void setColor(int color) {
        mArcPaint.setColor(color);
        postInvalidate();
    }

    public void setCircleColor(int color){
        mCirclePaint.setColor(color);
        postInvalidate();
    }

    public void setLoadingColor(int circleColor,int progressColor){
        mCirclePaint.setColor(circleColor);
        mArcPaint.setColor(progressColor);
        postInvalidate();
    }

    @Override
    protected ValueAnimator initAnim() {
        return ValueAnimator.ofFloat(0,360);
    }

    @Override
    public void startAnim() {
        startAnim(1000);
    }

    @Override
    public void startAnim(long time) {
        startAnim(ValueAnimator.INFINITE,ValueAnimator.RESTART,time);
    }

    @Override
    protected void onUpdateAnim(ValueAnimator animator) {
        degree = (float) animator.getAnimatedValue();
        postInvalidate();
    }

}
